package com.wyp168.juc.future;

import com.wyp168.juc.util.ExecutorsUtil;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName CompletableFutureDemo002
 * @Description TODO
 * @Author wyp168
 * @Date 2022/9/11 16:28
 */
public class CompletableFutureDemo002 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadPoolExecutor threadPoolExecutor = ExecutorsUtil.getThreadPoolExecutor();
//        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
//            try {
//                System.out.println(Thread.currentThread().getName());
//                TimeUnit.SECONDS.sleep(5);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }, threadPoolExecutor);
//        System.out.println(completableFuture.get());
//
//        //使用线程池后需要关闭线程池
//        threadPoolExecutor.shutdown();

        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println(Thread.currentThread().getName());
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "supplier result";
        }, threadPoolExecutor);
        System.out.println(completableFuture.get());
        //使用线程池资源后需要关闭资源
        threadPoolExecutor.shutdown();
    }
}
